package com.czy.owner.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.HotShopClassifyModel;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.hotshop.GoodsListActivity;
import com.czy.owner.ui.hotshop.HotShopWebActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.glide.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017/12/13.
 */

public class HotClassifyPagerAdapter extends PagerAdapter {
    private List<HotShopClassifyModel.ActivityZoneListBean> list = new ArrayList<>();
    private Context context;
    private static final int PLATFORM_TYPE = 0;
    private static int CLASSIFY_TYPE = 0;
    public HotClassifyPagerAdapter(Context context, List<HotShopClassifyModel.ActivityZoneListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }


    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_classify_view_pager, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_classify_view_pager);
        if (list != null && list.size() > 0) {
            GlideUtils.loadImage(context, list.get(position % list.size()).getShowToCategoryLogo(), imageView, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.default_banner, R.mipmap.default_banner);
            container.addView(view);

        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HotShopClassifyModel.ActivityZoneListBean activityZoneListBean = list.get(position%list .size());
                if ("category".equals(activityZoneListBean.getLinkType())) {
                    Intent intent = new Intent(context, GoodsListActivity.class);
                    intent.putExtra("fromType", CLASSIFY_TYPE);
                    intent.putExtra("categoryId", activityZoneListBean.getCategoryId());
                    intent.putExtra("goodsType", PLATFORM_TYPE);
                    intent.putExtra("title", activityZoneListBean.getTitle());
                    intent.putExtra("mFromType", Constants.GOODSSOURCE_STORE);
                    intent.putExtra("mFromTypeId", "0");
                    context.startActivity(intent);
                } else if ("url".equals(activityZoneListBean.getLinkType())) {
                    Intent intent = new Intent(context, HotShopWebActivity.class);
                    intent.putExtra("title", activityZoneListBean.getTitle());
                    intent.putExtra("url", activityZoneListBean.getUrl());
                    context.startActivity(intent);
                }


            }
        });
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}

